package com.cloud.controller;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import test.cloud.common.ResultVO;

import com.cloud.bean.UserInfo;
import com.cloud.service.ILoginService;

@RestController
@RequestMapping("/")
public class LoginController {

	@Autowired
	private ILoginService loginService;

	@RequestMapping(value = "login", method = RequestMethod.POST)
	public @ResponseBody ResultVO login(String name, String pass) {
		boolean b = loginService.isUserNameValid(name, pass);
		ResultVO rs = new ResultVO();
		if (b) {
			rs.setCode(1);
		} else {
			rs.setCode(0);
			rs.setMsg("Login error!");
		}
		return rs;
	}

	@RequestMapping(value = "regedit", method = RequestMethod.POST)
	public @ResponseBody ResultVO regedit(String name, String pass) {
		ResultVO rs = new ResultVO(1, "");
		UserInfo info = loginService.saveUser(name, pass);
		if (null != info) {
			rs.setCode(1);
			rs.setObj(info);
		} else {
			rs.setCode(0);
			rs.setMsg("Regedit Failed!");
		}
		return rs;
	}

	@RequestMapping(value = "list", method = RequestMethod.GET)
	public @ResponseBody ResultVO list() {
		ResultVO rs = new ResultVO(1, "");
		List<UserInfo> infos = loginService.getAllUsers();
		if (null != infos) {
			rs.setCode(1);
			rs.setObj(infos);
		} else {
			rs.setCode(0);
			rs.setMsg("Regedit Failed!");
		}
		return rs;
	}

	@RequestMapping(value = "passUpdate", method = RequestMethod.PUT)
	public @ResponseBody ResultVO updatePass(String name, String newPass) {
		ResultVO rs = new ResultVO();
		boolean b = loginService.updatePass(name, newPass);
		if (b) {
			rs.setCode(1);
		} else {
			rs.setCode(0);
			rs.setMsg("Modify Failed!");
		}
		return rs;
	}

	@RequestMapping(value = "user", method = RequestMethod.DELETE)
	public @ResponseBody ResultVO removeUser(String name) {
		ResultVO rs = new ResultVO();
		boolean b = loginService.removeUser(name);
		if (b) {
			rs.setCode(1);
		} else {
			rs.setCode(0);
			rs.setMsg("Delete Failed!");
		}
		return rs;
	}

	@RequestMapping(value = "")
	public ModelAndView login(PrintWriter out) {
		out.write("aaaaaaa");
		return new ModelAndView("welcome");
	}

	@RequestMapping(value = "upload", method = RequestMethod.POST)
	public @ResponseBody ResultVO handleFileUpload(
			@RequestParam("file") MultipartFile file,
			@RequestParam("fileName") String name) {

		String filePath = "D:\\workspace\\";
		ResultVO rs = new ResultVO();
		if (!file.isEmpty()) {
			byte[] bytes;
			try {
				bytes = file.getBytes();
				BufferedOutputStream stream = new BufferedOutputStream(
						new FileOutputStream(new File(filePath + file.getOriginalFilename())));
				stream.write(bytes);
				stream.close();
				rs.setCode(1);
				rs.setMsg("upload successfully!");
			} catch (IOException e) {
				e.printStackTrace();
				rs.setCode(1);
				rs.setMsg("upload error: file read error!");
			}
		}else{
			rs.setCode(0);
			rs.setMsg("File is empty!");
		}
		return rs;
	}
}
